// pages/artificer/order/order.js
var api = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      statusArr:[
       
      ],
      lists:[],
      page:1,
      lastPage:0,
      tab:[
        {name:'全部',selected:true,index:'-1'},
        {name:'待上门',selected:false,index:'0'},
        {name:'服务中',selected:false,index:'1'},
        {name:'已完成',selected:false,index:'2'},
        {name:'已评价',selected:false,index:'3'},
      ],
      tabNum:['-1'],
      type:0, //	 0=用户端订单列表,1=师傅端订单列表  
      status:'-1', //	0=用户端订单列表,1=师傅端订单列表  
      page_begin:'',
      location:{},
      longitude:'',
      latitude:'',
  },
  appointOrderStatusNum(){
    api.get('/appoint/orderStatusNum').then(res=>{
      let tabNum = this.data.tabNum;
      tabNum = tabNum.concat(res.options);
      this.setData({
        tabNum
      })
    })
  },
  changeTab(e){
    let index = e.currentTarget.dataset.index;
    let tab = this.data.tab;
    tab.map(res=>{
      res.selected = false;
    })

    tab[index].selected = true;
    this.setData({
      tab:tab,
      status:this.data.tab[index].index,
      page:1,
      lastPage:0,
      lists:[]
    })
    if(this.data.type==0){
      this.getOrder();
    }else if(this.data.type == 1){
      this.getArtificerOrder();
    }
   
  },
  //用户确认
  userFinish(e){
    let order_id = e.currentTarget.dataset.order_id;
    let index = e.currentTarget.dataset.index;
    api.post('/appoint/finish/'+order_id).then(res=>{
      let lists = this.data.lists;
      lists[index].state = 1;
      this.setData({
        lists
      })
    })
  },
  getCouponAllows(){

    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/coupon/allows',{
      service_id:this.data.service_id,
      longitude:this.data.longitude,
      latitude:this.data.latitude,
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  //取消下单
  appointCancel(e){

    let lists = this.data.lists;
    let index = e.currentTarget.dataset.index;
    lists[index].is_apply = 1;
    api.post('/appoint/cancel/'+this.data.lists[index].order_id).then(res=>{
      this.setData({
        lists
      })
    })
  },
  //客户端订单
  getOrder(){

    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/appoint/orders',{
      type:this.data.type,
      status:this.data.status
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  //师傅端订单
  getArtificerOrder(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
 
    api.get('/appoint/orders',{
      type:this.data.type,
      status:this.data.status,
      longitude:this.data.longitude,
      latitude:this.data.latitude
    }).then(res=>{
     
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  //确定到达
  appointArrive(e){
    let orderId = e.currentTarget.dataset.order_id;
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/car_order/check_car/check_car?type=0&order_id='+orderId+'&index='+index,
    })
  },
    //确定完成
    appointConfirm(e){
      let orderId = e.currentTarget.dataset.order_id;
      let index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: '/pages/car_order/check_car/check_car?type=1&order_id='+orderId+'&index='+index,
      })
    },

  goToDetial(e){
    let order_id = e.currentTarget.dataset.order_id;
    wx.navigateTo({
      url: '/pages/car_order/order_detail/order_detail?order_id='+order_id+'&page_begin='+this.data.page_begin,
    })
  },
  tabUrl(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let url = that.data.menu[index].url;
    wx.redirectTo({
      url: url,
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('page',options)
      if(wx.getStorageSync('Location')){
        let location =  wx.getStorageSync('Location');
        if(location){
          this.setData({
            longitude:location.longitude,
            latitude:location.latitude
          })
        }
      }
      if(options.page_begin){
        this.setData({
          page_begin:options.page_begin,
          type:options.type
        })
      }
      if(options.type == 0){
        this.getOrder(); //用户端
      }else if(options.type == 1){
        this.getArtificerOrder(); //师傅端
      }
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    if(this.data.page_begin=="artificer"){
      let menu = this.selectComponent("#menu");
      menu.setMenu()
    }
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.appointOrderStatusNum();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrder();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})